package com.basiclib.base

import android.annotation.SuppressLint
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.content.pm.ActivityInfo
import android.graphics.Color
import android.net.ConnectivityManager
import android.os.Build
import android.os.Bundle
import android.view.ActionMode
import android.view.View
import android.view.View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
import android.view.WindowManager
import androidx.appcompat.app.AppCompatActivity
import com.basiclib.BasicLibComponant
import xc.common.framework.listener.NoShakeOnClickListener
import xc.common.tool.utils.LibAppUtils
import xc.common.tool.utils.SWLog
import java.util.*

abstract class BaseActivity() : AppCompatActivity(), NoShakeOnClickListener {

    abstract fun getLayoutId(): Int
    abstract fun initView()
    override fun onNoShakeClick(v: View){
    }

    abstract fun loadData()

    var delayLoadData = false


    override fun attachBaseContext(newBase: Context?) {
        if (BasicLibComponant.attachBaseContext == null) {
            super.attachBaseContext(newBase)
        } else {
            super.attachBaseContext(BasicLibComponant.attachBaseContext?.attachBaseContext(newBase))
        }
    }
    /**
     * savedInstanceState 不为空时初始页面有bug，所有这里保存起来，让子类判断
     */
    var savedInstanceState: Bundle? = null

    var IslistenerNet = false

    @SuppressLint("SourceLockedOrientationActivity")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        this.savedInstanceState = savedInstanceState
//        Sw.i(AppManager.getAppManager().currentActivity().localClassName)
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT)
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
            window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                window.statusBarColor = Color.TRANSPARENT
            }
        }
        if (getLayoutId() != 0) {
            setContentView(getLayoutId())
        }
        initView()

        if (!delayLoadData) {
            loadData()
        }
        if (IslistenerNet) {
            initNetListener()
        }
    }

    private fun initNetListener() {
        var tag = true
        if (!LibAppUtils.isNetworkConnected()) {
            showNullNetView()
        }

        var netReceiver = object : BroadcastReceiver() {
            override fun onReceive(context: Context?, intent: Intent?) {
                if (tag == true) {
                    tag = false
                    return
                }
                if (LibAppUtils.isNetworkConnected()) {
                    showHasNetView()
                    loadData()
                } else {
                    showNullNetView()
                }
            }
        }

        var intentFilter = IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION)
        registerReceiver(netReceiver, intentFilter)
    }

    open fun showHasNetView() {

    }

    open fun showNullNetView() {

    }

    override fun onDestroy() {
        super.onDestroy()
        SWLog.e("onDestroy-> ${localClassName}")

    }

}